package com.cyou.nad.tag.table.pagebar;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.mock.web.MockHttpServletRequest;

import com.cyou.nad.dao.Pagination;
import com.cyou.nad.datastore.query.IJdbcPage;
import com.cyou.nad.datastore.query.JdbcPage;



/**
 * 
 * Title: 分页导航条的简单实现   <br>
 * 效果:共63项  第1/4页  首页 上页 下页 末页   <br>
 * @author luzm    <br>
 */
public final class JdbcPageBar extends AbstractPageBar implements PageBar {
	public String getHTML(HttpServletRequest req, Pagination page, String formAction) {
		Object obj = new Object();
		String formId = obj.hashCode()+"";
		String urlPageNo = req.getParameter(REQUEST_PARAMETER_PAGE);
		int currentPage = StringUtils.isBlank(urlPageNo) ? 1 : new Integer(urlPageNo).intValue();
		int totalPages = page.getTotalPageCount();
		int totalRows = page.getTotalCount();

		StringBuffer form = builderSearchForm(req, formId, formAction, totalRows, page.getPageSize());
		StringBuffer navigation = getNav(formId, currentPage, totalPages, totalRows,page.getPageSize());
		return form.append(navigation).toString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see table.PageBar#getHTML()
	 */
	public StringBuffer getNav(String formId, int currentPage, int totalPages, Object totalRows,int pageSize) {
		int showCurrentPage=currentPage;
		if (currentPage > totalPages)
			currentPage = totalPages;
		if (currentPage < 1)
			currentPage = 1;
	
		StringBuffer html = new StringBuffer();
		html.append("每页显示 <select id='"+formId+"PageSize' onchange='goto3Page"+formId+"()'> " );
		if(pageSize==10){
			html.append(		"<option value='10' selected>10</option>" );
		}else{
			html.append(		"<option value='10'>10</option>" );
		}
		if(pageSize==20){
			html.append(		"<option value='20' selected>20</option>" );
		}else{
			html.append(		"<option value='20'>20</option>" );
		}
		if(pageSize==50){
			html.append(		"<option value='50' selected>50</option>" );
		}else{
			html.append(		"<option value='50'>50</option>" );
		}
		if(pageSize==100){
			html.append(		"<option value='100' selected>100</option>" );
		}else{
			html.append(		"<option value='100'>100</option>" );
		}

		html.append(		"</select>条记录 ");
		html.append("共"+totalRows);
		html.append("个 &nbsp;");
		if (currentPage == 1) {
			html.append("<font color=#999999>首页 上页 </font>");
		} else {
			html.append("<a href=\"javascript:gotoPage"+formId+"('" + formId + "',1)\">首页</a> ");
			html.append("<a href=\"javascript:gotoPage"+formId+"('" + formId + "'," + (currentPage - 1) + ")\">上页</a> ");
		}

		if (currentPage >= totalPages) {
			html.append("<font color=#999999>下页 末页</font>");
		} else {
			html.append("<a href=\"javascript:gotoPage"+formId+"('" + formId + "'," + (currentPage + 1) + ")\">下页</a> ");
			html.append("<a href=\"javascript:gotoPage"+formId+"('" + formId + "'," + totalPages + ")\">末页</a> ");
		}
		html.append("&nbsp;");
		html.append(showCurrentPage);
		html.append("/");
		html.append(totalPages);
		html.append("页&nbsp;&nbsp;");
		html.append("跳转到<input type='text' id='"+formId+"PageNo' size='1'/> <input type='button' value='go' onclick='goto2Page"+formId+"()'/>");
		html.append("<script type = 'text/javaScript'>\r\n");
		html.append("function gotoPage"+formId+"(formId,pageNo,pageSize){ if(!parseInt(pageNo)||pageNo.length>8){alert('页数不正确，请正确输入');return false;}" +
						"if(pageSize){ " +
						"	var form = document.getElementById('"+formId+"');" +
						"	form."+REQUEST_PARAMETER_PAGE_SIZE+".value = pageSize;" +
						"}" +
						"var pageId = '"+ REQUEST_PARAMETER_PAGE+ formId+ "';" +
						"var pageInput = document.getElementById(pageId);	" +
						"pageInput.value=parseInt(pageNo);document.getElementById(formId).submit();}");
		html.append("function goto2Page"+formId+"(){ var pageNo= document.getElementById('"+formId+"PageNo'); gotoPage"+formId+"('" + formId + "',pageNo.value)}") ;
		html.append("	function goto3Page"+formId+"(){");
		html.append("		var select = document.getElementById('"+formId+"PageSize');");
		html.append("		var options= select.childNodes;");
		html.append("		var pageSize;");
		html.append("		for(var i=0;i<options.length;i++){");
		html.append("			if(options[i].selected){");
		html.append("				pageSize=options[i].value;");
		html.append("			}");
		html.append("		}");
		html.append("		gotoPage"+formId+"('" + formId + "',"+currentPage+",pageSize)");
		html.append("	}");	
		html.append("</script>");
		return html;
	}

	public static void main(String[] adf) {
		MockHttpServletRequest req = new MockHttpServletRequest();
		req.setParameter("param1", "param1");
		req.setParameter("param2", new String[] { "param21", "param22" });
		req.setParameter("param3", new String[] { "中文", "中文2" });
		IJdbcPage page = new JdbcPage() {
			
			@SuppressWarnings("unused")
			private static final long serialVersionUID = 216159127657006090L;

			public int getCurrentPageNo() {
				return 0;
			}
			public int getPageSize() {
				return 20;
			}

			public int getTotalCount() {
				return 200;
			}

			public int getTotalPageCount() {
				return 20;
			}

			public boolean hasNextPage() {
				return false;
			}

			public boolean hasPreviousPage() {
				return false;
			}
		};
		String html = new JdbcPageBar().getHTML(req, page, " formAction");
		System.out.println(html);
	}
}